﻿using System;
using System.Collections.Generic;
using System.Linq;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using System.ComponentModel.DataAnnotations;
using HengTong.Model.Db.Hr.Bill;
using HengTong.Model.Extension;
using TiaoTaoHelper.Wtm.Models;


namespace HengTong.ViewModel.Hr.SysHrOnboardingRequestBillVms
{
    public partial class
        SysHrOnboardingRequestBillListVm : BaseListVm<SysHrOnboardingRequestBillView,
        SysHrOnboardingRequestBillSearcher>
    {
        protected override IEnumerable<IGridColumn<SysHrOnboardingRequestBillView>> InitGridHeader()
        {
            return new List<GridColumn<SysHrOnboardingRequestBillView>>
            {
                this.MakeGridHeader(x => x.PersonMedicalName),
                this.MakeGridHeader(x => x.SysPersonTrainExamName),
                this.MakeGridHeader(x => x.BillCode),
                this.MakeGridHeader(x => x.BillStatus),
                this.MakeGridHeader(x => x.WorkflowStatus),
                this.MakeGridHeader(x => x.Remark),
                this.MakeGridHeader(x => x.Approver),
                this.MakeGridHeader(x => x.ApprovalTime),
                this.MakeGridHeader(x => x.Name),
                this.MakeGridHeader(x => x.StaffNo),
                this.MakeGridHeader(x => x.BirthDate),
                this.MakeGridHeader(x => x.Age),
                this.MakeGridHeader(x => x.Gender),
                this.MakeGridHeader(x => x.Ethnicity),
                this.MakeGridHeader(x => x.HighestEducation),
                this.MakeGridHeader(x => x.MaritalStatus),
                this.MakeGridHeader(x => x.PhotoId).SetFormat(PhotoIdFormat),
                this.MakeGridHeader(x => x.IdCardNo),
                this.MakeGridHeader(x => x.IdCardFrontPhotoId).SetFormat(IdCardFrontPhotoIdFormat),
                this.MakeGridHeader(x => x.IdCardBackPhotoId).SetFormat(IdCardBackPhotoIdFormat),
                this.MakeGridHeader(x => x.HouseholdRegistration),
                this.MakeGridHeader(x => x.NativePlace),
                this.MakeGridHeader(x => x.PoliticalStatus),
                this.MakeGridHeader(x => x.PartyJoinDate),
                this.MakeGridHeader(x => x.PartyBranch),
                this.MakeGridHeader(x => x.ArchiveLocation),
                this.MakeGridHeader(x => x.ArchiveNo),
                this.MakeGridHeader(x => x.ArchiveTransferOutDate),
                this.MakeGridHeader(x => x.ArchiveTransferInDate),
                this.MakeGridHeader(x => x.HomeAddress),
                this.MakeGridHeader(x => x.Phone),
                this.MakeGridHeader(x => x.Email),
                this.MakeGridHeader(x => x.SalaryBank),
                this.MakeGridHeader(x => x.SalaryAccount),
                this.MakeGridHeader(x => x.BonusBank),
                this.MakeGridHeader(x => x.BonusAccount),
                this.MakeGridHeader(x => x.OrgName),
                this.MakeGridHeader(x => x.GroupName),
                this.MakeGridHeader(x => x.JobPositionName),
                this.MakeGridHeader(x => x.ResumeId).SetFormat(ResumeIdFormat),
                this.MakeGridHeader(x => x.HireDate),
                this.MakeGridHeader(x => x.ConfirmationDate),
                this.MakeGridHeader(x => x.EmploymentType),
                this.MakeGridHeader(x => x.ContractType),
                this.MakeGridHeader(x => x.ContractNo),
                this.MakeGridHeader(x => x.ContractStartDate),
                this.MakeGridHeader(x => x.ContractEndDate),
                this.MakeGridHeader(x => x.ResignationDate),
                this.MakeGridHeader(x => x.ResignationReason),
                this.MakeGridHeader(x => x.ResignationType),
                this.MakeGridHeader(x => x.State),
                this.MakeGridHeader(x => x.BloodType),
                this.MakeGridHeader(x => x.Hobbies),
                this.MakeGridHeader(x => x.CanApprove).SetHide().SetFormat((a, b) =>
                {
                    if (a.CanApprove)
                    {
                        return "true";
                    }
                    else
                    {
                        return "false";
                    }
                }),

                this.MakeGridHeaderAction(width: 200)
            };
        }

        private List<ColumnFormatInfo> PhotoIdFormat(SysHrOnboardingRequestBillView entity, object val)
        {
            return new List<ColumnFormatInfo>
            {
                ColumnFormatInfo.MakeDownloadButton(ButtonTypesEnum.Button, entity.PhotoId),
                ColumnFormatInfo.MakeViewButton(ButtonTypesEnum.Button, entity.PhotoId, 640, 480),
            };
        }

        private List<ColumnFormatInfo> IdCardFrontPhotoIdFormat(SysHrOnboardingRequestBillView entity, object val)
        {
            return new List<ColumnFormatInfo>
            {
                ColumnFormatInfo.MakeDownloadButton(ButtonTypesEnum.Button, entity.IdCardFrontPhotoId),
                ColumnFormatInfo.MakeViewButton(ButtonTypesEnum.Button, entity.IdCardFrontPhotoId, 640, 480),
            };
        }

        private List<ColumnFormatInfo> IdCardBackPhotoIdFormat(SysHrOnboardingRequestBillView entity, object val)
        {
            return new List<ColumnFormatInfo>
            {
                ColumnFormatInfo.MakeDownloadButton(ButtonTypesEnum.Button, entity.IdCardBackPhotoId),
                ColumnFormatInfo.MakeViewButton(ButtonTypesEnum.Button, entity.IdCardBackPhotoId, 640, 480),
            };
        }

        private List<ColumnFormatInfo> ResumeIdFormat(SysHrOnboardingRequestBillView entity, object val)
        {
            return new List<ColumnFormatInfo>
            {
                ColumnFormatInfo.MakeDownloadButton(ButtonTypesEnum.Button, entity.ResumeId),
                ColumnFormatInfo.MakeViewButton(ButtonTypesEnum.Button, entity.ResumeId, 640, 480),
            };
        }

        public override void AfterDoSearcher()
        {
            if (Wtm.LoginUserInfo != null)
            {
                List<SysWorkflow> ids = GetMyApproves();
                if (ids.Any())
                {
                    foreach (var item in EntityList)
                    {
                        var m = ids.Find(x => x.ModelId == item.GetID().ToString());
                        if (m != null && item.WorkflowStatus == WorkflowStatus.审核中)
                        {
                            item.CanApprove = true;
                        }
                    }
                }
            }
            
            EntityList.ForEach(e =>
            {
                e.CreateBy = Wtm.FormatUsername(e.CreateBy);
                e.UpdateBy = Wtm.FormatUsername(e.UpdateBy);
                e.Approver = Wtm.FormatUsername(e.Approver);
            });

            base.AfterDoSearcher();
        }


        public override IOrderedQueryable<SysHrOnboardingRequestBillView> GetSearchQuery()
        {
            var query = DC.Set<SysHrOnboardingRequestBill>()
                .CheckContain(Searcher.BillCode, x => x.BillCode)
                .CheckEqual(Searcher.BillStatus, x => x.BillStatus)
                .CheckEqual(Searcher.WorkflowStatus, x => x.WorkflowStatus)
                .CheckContain(Searcher.Approver, x => x.Approver)
                .CheckBetween(Searcher.ApprovalTime?.GetStartTime(), Searcher.ApprovalTime?.GetEndTime(),
                    x => x.ApprovalTime, includeMax: false)
                .CheckContain(Searcher.Name, x => x.Name)
                .CheckContain(Searcher.StaffNo, x => x.StaffNo)
                .CheckEqual(Searcher.Gender, x => x.Gender)
                .CheckEqual(Searcher.Ethnicity, x => x.Ethnicity)
                .CheckContain(Searcher.IdCardNo, x => x.IdCardNo)
                .CheckContain(Searcher.ArchiveNo, x => x.ArchiveNo)
                .CheckContain(Searcher.Phone, x => x.Phone)
                .CheckEqual(Searcher.OrgId, x => x.OrgId)
                .CheckEqual(Searcher.GroupId, x => x.GroupId)
                .CheckEqual(Searcher.JobPositionId, x => x.JobPositionId)
                .CheckEqual(Searcher.EmploymentType, x => x.EmploymentType)
                .CheckEqual(Searcher.ContractType, x => x.ContractType)
                .CheckEqual(Searcher.State, x => x.State)
                .Select(x => new SysHrOnboardingRequestBillView
                {
                    ID = x.ID,
                    PersonMedicalName = x.PersonMedical.CreateBy,
                    SysPersonTrainExamName = x.SysPersonTrainExam.CreateBy,
                    BillCode = x.BillCode,
                    BillStatus = x.BillStatus,
                    WorkflowStatus = x.WorkflowStatus,
                    Remark = x.Remark,
                    Approver = x.Approver,
                    ApprovalTime = x.ApprovalTime,
                    Name = x.Name,
                    StaffNo = x.StaffNo,
                    BirthDate = x.BirthDate,
                    Age = x.Age,
                    Gender = x.Gender,
                    Ethnicity = x.Ethnicity,
                    HighestEducation = x.HighestEducation,
                    MaritalStatus = x.MaritalStatus,
                    PhotoId = x.PhotoId,
                    IdCardNo = x.IdCardNo,
                    IdCardFrontPhotoId = x.IdCardFrontPhotoId,
                    IdCardBackPhotoId = x.IdCardBackPhotoId,
                    HouseholdRegistration = x.HouseholdRegistration,
                    NativePlace = x.NativePlace,
                    PoliticalStatus = x.PoliticalStatus,
                    PartyJoinDate = x.PartyJoinDate,
                    PartyBranch = x.PartyBranch,
                    ArchiveLocation = x.ArchiveLocation,
                    ArchiveNo = x.ArchiveNo,
                    ArchiveTransferOutDate = x.ArchiveTransferOutDate,
                    ArchiveTransferInDate = x.ArchiveTransferInDate,
                    HomeAddress = x.HomeAddress,
                    Phone = x.Phone,
                    Email = x.Email,
                    SalaryBank = x.SalaryBank,
                    SalaryAccount = x.SalaryAccount,
                    BonusBank = x.BonusBank,
                    BonusAccount = x.BonusAccount,
                    OrgName = x.Org.Name,
                    GroupName = x.Group.GroupName,
                    JobPositionName = x.JobPosition.Name,
                    ResumeId = x.ResumeId,
                    HireDate = x.HireDate,
                    ConfirmationDate = x.ConfirmationDate,
                    EmploymentType = x.EmploymentType,
                    ContractType = x.ContractType,
                    ContractNo = x.ContractNo,
                    ContractStartDate = x.ContractStartDate,
                    ContractEndDate = x.ContractEndDate,
                    ResignationDate = x.ResignationDate,
                    ResignationReason = x.ResignationReason,
                    ResignationType = x.ResignationType,
                    State = x.State,
                    BloodType = x.BloodType,
                    Hobbies = x.Hobbies,
                    CreateTime = x.CreateTime // 添加CreateTime属性
                })
                .OrderByDescending(x => x.CreateTime);
            return query;
        }
    }

    public class SysHrOnboardingRequestBillView : SysHrOnboardingRequestBill
    {
        [Display(Name = "_Admin.CreateBy")] public String PersonMedicalName { get; set; }
        [Display(Name = "_Admin.CreateBy")] public String SysPersonTrainExamName { get; set; }
        [Display(Name = "组织名称")] public String OrgName { get; set; }
        [Display(Name = "_Admin.GroupName")] public String GroupName { get; set; }
        [Display(Name = "岗位名称")] public String JobPositionName { get; set; }
        public bool CanApprove { get; set; }
    }
}